import { Component, OnInit, ViewChild } from '@angular/core';
import { NavController, IonInfiniteScroll, PickerController, MenuController, IonRefresher, IonContent } from '@ionic/angular';
import { ToolService } from 'src/app/service/tool.service';
import { UrlService } from 'src/app/service/url.service';
import { HttpService } from 'src/app/service/http.service';
import { ConfigService } from 'src/app/service/config.service';
import { PopwinService } from 'src/app/service/popwin.service';
import { EventService } from 'src/app/service/event.service';
import { Storage } from '@ionic/storage';
import * as $ from 'jquery';

@Component({
  selector: 'app-index',
  templateUrl: './index.page.html',
  styleUrls: ['./index.page.scss'],
})
export class IndexPage implements OnInit {
  minValue = "2019-01-01";
  selectDate = ToolService.getNowFormatDate();
  selectTempDate = ToolService.getNowFormatDate();
  tabarData = { "cycleid": "", "sdate": "", "balls": "", "sum": "", "nn": "", "dx": "", "ds": "", "gylh": "", "show": "", "tm": "", "zh": "","bsl":"" };
  showStateData = {};
  listData = [];
  public isRes = false;
  public ruleInfo = { "content": "", "notes_title": "", "statement": "" };
  public disabled = false;//是否有上拉
  public lotType = "";
  private tempLotType = "";
  public lotKey = "";
  public tempLotKey = ""; //用于模板中的判断
  public tempEventLotKey = "";//是回传过来的值
  private isFirst = true;
  private page = 1;
  @ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;
  @ViewChild(IonRefresher) ionRefresher: IonRefresher;
  constructor(private navCtrl: NavController, private http: HttpService, private popWin: PopwinService, private pickercontroller: PickerController, private eventS: EventService, private menu: MenuController, private store: Storage) {

  }

  ngOnInit() {
    this.initLotType();
    this.initEvent();//初始化event
  }

  initState() {
    if (this.tabarData.balls) {
      this.showStateFun("balls");
      return;
    } else if (this.tabarData.dx) {
      this.showStateFun("dx");
      return;
    } else if (this.tabarData.ds) {
      this.showStateFun("ds");
      return;
    }
  }

  toLotMenuPage() {
    // this.menu.enable(true, 'lotTwoMenu');
    // this.menu.open('lotTwoMenu');
    $(".lotTwoMenu").css("display", "block");
    this.eventS.publish("getMenuUserinfo");
    this.eventS.publish("getSlideMenu", false);
  }
  /**
   * 初始化获取类别的第一个数据，返回第一个数据的key
   */
  async initLotType() {
    let tempData
    await this.store.get(ConfigService.ALL_LOT_STORE_KEY).then((res) => {
      tempData = res;
    });
    if (ToolService.isEmpty(tempData)) {
      this.lotType = "六合彩";
      this.lotKey = "lhc";
    }
    let data = tempData;
    if (data.length <= 0) {
      this.lotType = "六合彩";
      this.lotKey = "lhc";
    } else {
      this.lotType = data[0].name;
      this.lotKey = data[0].key;
    }
    if (!ToolService.isEmpty($("app-root").attr("code")) && !ToolService.isEmpty($("app-root").attr("name"))) {
      this.page = 1;
      this.selectDataFun($("app-root").attr("code"), $("app-root").attr("name"), this.selectDate.substr(0, 10), 1, "", false);
    }
    // this.selectDataFun(this.lotKey, this.lotType, this.selectDate.substr(0, 10), this.page, "", false);
  }
  /**
   * 筛选的类别
   * @param type 
   */
  onSelectType(type: string) {// balls号码,dx大小,ds单双,gylh冠亚/龙虎,sum总和,nn牛牛，
    this.showStateFun(type);
  }
  showStateFun(type: string) {
    console.log("tempLotKey:" + this.tempLotKey + "-lotKey:" + this.lotKey);
    let temp = this.tempEventLotKey ? this.tempEventLotKey : this.lotKey;
    this.tempLotKey = temp;
    console.log("-----------------当前彩种:", temp);
    switch (type) {
      case 'balls': {
        // 点击号码显示号码和龙虎的数据
        // if (temp == 'pk8' || temp == 'pt8' || temp == 'pks' || temp == 'sg8' || temp == 'pkt') {
        //   this.showStateData = { "balls": true, "sum": true, "nn": false, "dx": false, "ds": false, "gylh": false, "tm": false };
        //   break;
        // } else {
        //   this.showStateData = { "balls": true, "sum": false, "nn": false, "dx": false, "ds": false, "gylh": false, "tm": false };
        //   break;
        // }
        this.showStateData = { "balls": true, "sum": false, "nn": false, "dx": false, "ds": false, "gylh": false, "tm": false, "zh": false, "bsl": false };
        break;
      }
      case 'dx': {
        this.showStateData = { "balls": false, "sum": false, "nn": false, "dx": true, "ds": false, "gylh": false, "tm": false, "zh": false, "bsl": false };
        break;
      }
      case 'ds': {
        this.showStateData = { "balls": false, "sum": false, "nn": false, "dx": false, "ds": true, "gylh": false, "tm": false, "zh": false, "bsl": false };
        break;
      }
      case 'gylh': {
        this.showStateData = { "balls": false, "sum": false, "nn": false, "dx": false, "ds": false, "gylh": true, "tm": false, "zh": false, "bsl": false };
        break;
      }
      case 'sum': {
        this.showStateData = { "balls": false, "sum": true, "nn": false, "dx": false, "ds": false, "gylh": false, "tm": false, "zh": false, "bsl": false };
        break;
      }
      case 'nn': {
        this.showStateData = { "balls": false, "sum": false, "nn": true, "dx": false, "ds": false, "gylh": false, "tm": false, "zh": false, "bsl": false };
        break;
      }
      case 'tm': {
        this.showStateData = { "balls": false, "sum": false, "nn": false, "dx": false, "ds": false, "gylh": false, "tm": true, "zh": false, "bsl": false };
        break;
      }
      case 'zh': {
        this.showStateData = { "balls": false, "sum": false, "nn": false, "dx": false, "ds": false, "gylh": false, "tm": false, "zh": true, "bsl": false };
        break;
      }
      case 'bsl': {
        this.showStateData = { "balls": false, "sum": false, "nn": false, "dx": false, "ds": false, "gylh": false, "tm": false, "zh": false, "bsl": true };
        break;
      }
    }
  }
  /**
   * 数据加载初始化
   */
  initLoadData(isMenu?: boolean) {
    this.page = 1;
    this.infiniteScroll.disabled = false;
    this.selectDataFun(isMenu ? this.tempEventLotKey : this.lotKey, isMenu ? this.tempLotType : this.lotType, this.selectDate.substr(0, 10), 1, "", false);
  }
  /**
   * 选择时间
   */
  selectTime() {
    let comRes = ToolService.compareDate(this.selectDate.substr(0, 10), ToolService.getNowFormatDate());
    if (comRes) {
      this.popWin.toast("选择时间不能够大于当前时间");
      return;
    }
    if (this.selectTempDate != this.selectDate.substr(0, 10)) {
      this.selectTempDate = this.selectDate.substr(0, 10);
      this.initLoadData();
    }
  }
  /**
   * 获取数据 
   * isMore  为ture上拉加载更多
   */
  selectDataFun(topcode, topTypeName, time, page, infinite?: any, isMore: boolean = false) {
    let that = this;
    let obj = "{\"topcode\":\"" + topcode + "\",\"time\":\"" + time + "\",\"page\":\"" + page + "\",\"pageSize\":\"35\"}";
    console.log("param:", obj, "key:", topcode, "name:", topTypeName);
    this.http.get(UrlService.LOTTERY_INDEX, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      that.ionRefresher.complete();
      if (infinite) {
        that.infiniteScroll.complete();
      }
      if (data.code == ConfigService.SUCCESS_CODE) {
        let temp = data.data;
        let tt = ToolService.aesDecodeToJson(temp);
        that.lotKey = topcode;
        that.lotType = topTypeName;
        if (isMore == false && tt.data.length == 0) {
          that.popWin.toast("暂无开奖结果");
          that.listData = [];
          that.tabarData = { "cycleid": "", "sdate": "", "balls": "", "sum": "", "nn": "", "dx": "", "ds": "", "gylh": "", "show": tt.tabar.show, "tm": "", "zh": "","bsl":"" };
          that.isRes = false;
          return;
        }
        that.isRes = true;
        if (!isMore) {//下拉或第一次加载
          that.tabarData = tt.tabar;
          that.listData = [];
          that.listData = tt.data;
          that.disabled = false;
          that.initState();
        } else {
          if (tt.data.length > 0) {
            that.listData = that.listData.concat(tt.data);
          } else {
            that.infiniteScroll.disabled = true;
            that.disabled = true;
            that.popWin.toast("数据已经加载完成");
          }
        }
        console.log(tt);
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
      that.ionRefresher.complete();
      if (infinite) {
        that.infiniteScroll.complete();
      }
    });
  }
  /**
   * 下拉刷新更多
   * @param event 
   */
  doRefresh(event) {
    this.initLoadData();
  }

  initEvent() {
    let that = this;
    this.eventS.subscribe("selectCommonTab4", function (index) {
      if (index == 4) {
        if (!ToolService.isEmpty($("app-root").attr("code")) && !ToolService.isEmpty($("app-root").attr("name"))) {
          that.tempEventLotKey = $("app-root").attr("code");
          that.tempLotType = $("app-root").attr("name");
          that.initLoadData(true);
        } else {
          that.initLoadData();
        }
      }
    }, true);
    this.eventS.subscribe("selectLotMenu", function (key, name, lotteryId) {
      if (that.lotType == name) {
        return;
      }
      that.tempEventLotKey = key;
      that.tempLotType = name;
      that.initLoadData(true);
    }, true);
  }
  /**
   * 上拉刷新
   * @param event 
   */
  loadData(event) {
    this.page++;
    this.selectDataFun(this.lotKey, this.lotType, this.selectDate.substr(0, 10), this.page, event, true);
  }

  toLoginPage() {
    this.navCtrl.navigateForward("/login");
  }

  fillerBsl(val) {
    if (val === "前(多)" || val === "单(多)") {
      return "qdd";
    } else if (val === "单双(和)" || val === "前后(和)") {
      return "qdh";
    } else if (val === "双(多)" || val === "后(多)") {
      return "shd";
    } else if (val === "金") {
      return "jin";
    } else if (val === "木") {
      return "mu";
    } else if (val === "水") {
      return "shui";
    } else if (val === "火") {
      return "huo";
    } else if (val === "土") {
      return "tu";
    }
  }
}
